package ins.wink.controller;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ins.wink.entity.Store;
import ins.wink.service.StoreService;
import ins.wink.utils.MD5Utils;

/**
 * 仓库controller层
 */
@Controller
public class StoreController {
	//注册仓库service层
	@Autowired
	private StoreService storeService;
	
	//查询所有
	@RequestMapping(value="/storeAction_list",method=RequestMethod.GET)
	public String storeAction_list(HttpServletRequest request) {
		List<Store> list = storeService.storeAlls();
		request.setAttribute("listAll", list);
		return "WEB-INF/store/list";
		
	}
	//根据id查询
	@RequestMapping(value="/toedit",method=RequestMethod.GET)
	public String toedit(@RequestParam(value="id") String id,HttpServletRequest request) {
		Store store = storeService.listStoreIds(id);
		request.setAttribute("store", store);
		return "WEB-INF/store/edit";
	}
	//修改					
	@RequestMapping(value="/storeupdate",method=RequestMethod.POST)
	@ResponseBody
	public boolean storeupdate(Store store) {
		storeService.updateStores(store);
		return true;
	}
	//删除
	@RequestMapping(value="/stroedelete",method=RequestMethod.POST)
	@ResponseBody
	public boolean stroedelete(@RequestParam(value="id") String id) {
		storeService.stroedeletes(id);
		return true;
	}
	//创建新仓库		
	@RequestMapping(value="/storeadd")
	public String storeadd() {
		return "WEB-INF/store/add";
	}
	//查看仓库名是否存在
	@RequestMapping(value="/storeAction_findByName")
	@ResponseBody
	public String storeAction_findByName(@RequestParam(value="name") String name) {
		Store store = storeService.selectStoreName(name);
		if(store==null) {
			return "ok";
		}
		return "no";
	}
	//添加操作
	@RequestMapping(value="/storeAction_add",method=RequestMethod.POST)
	public String storeAction_add(Store store) {
		//对属性id进行加密
		String id = MD5Utils.md5(UUID.randomUUID().toString());
		//新建实体类重新赋值
		Store store1 = new Store();
		store1.setId(id);
		store1.setName(store.getName());
		store1.setAddress(store.getAddress());
		store1.setManager(store.getManager());
		//添加
		storeService.insertStores(store1);
		return "redirect:/storeAction_list";
	}
	//部门查询goodsAction_findAllStore
}
